use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
    println!("Welcome to guess_number game!");

    let secret_number = rand::thread_rng().gen_range(1, 101);
    //println!("The secret number is: {}", secret_number);

    loop {
        println!("Please input your guess.");
        // 创建一个可变的guess变量
        let mut guess = String::new();
        // 从命令行接收用户输入
        io::stdin().read_line(&mut guess)
            .expect("Failed to read line");
        //将用户的输入转换成u32类型，根据定义guess变量时类型u32来决定转换后的类型
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue
        };

        println!("you guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}
